home *** CD-ROM | disk | FTP | other *** search
/ Danny Amor's Online Library / Danny Amor's Online Library - Volume 1.iso / html / rfc / rfcxxxx / rfc1589 < prev    next >
Text File  |  1995-07-26  |  92KB  |  2,076 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7. Network Working Group                                           D. Mills
  8. Request for Comments: 1589                        University of Delaware
  9. Category: Informational                                       March 1994
  10.  
  11.  
  12.                 A Kernel Model for Precision Timekeeping
  13.  
  14. Status of this Memo
  15.  
  16.    This memo provides information for the Internet community.  This memo
  17.    does not specify an Internet standard of any kind.  Distribution of
  18.    this memo is unlimited.
  19.  
  20. Overview
  21.  
  22.    This memorandum describes an engineering model which implements a
  23.    precision time-of-day function for a generic operating system. The
  24.    model is based on the principles of disciplined oscillators and
  25.    phase-lock loops (PLL) often found in the engineering literature. It
  26.    has been implemented in the Unix kernel for several workstations,
  27.    including those made by Sun Microsystems and Digital Equipment. The
  28.    model changes the way the system clock is adjusted in time and
  29.    frequency, as well as provides mechanisms to discipline its frequency
  30.    to an external precision timing source. The model incorporates a
  31.    generic system-call interface for use with the Network Time Protocol
  32.    (NTP) or similar time synchronization protocol. The NTP Version 3
  33.    daemon xntpd operates with this model to provide synchronization
  34.    limited in principle only by the accuracy and stability of the
  35.    external timing source.
  36.  
  37.    This memorandum does not obsolete or update any RFC. It does not
  38.    propose a standard protocol, specification or algorithm. It is
  39.    intended to provoke comment, refinement and alternative
  40.    implementations. While a working knowledge of NTP is not required for
  41.    an understanding of the design principles or implementation of the
  42.    model, it may be helpful in understanding how the model behaves in a
  43.    fully functional timekeeping system. The architecture and design of
  44.    NTP is described in [1], while the current NTP Version 3 protocol
  45.    specification is given in RFC-1305 [2] and a subset of the protocol,
  46.    the Simple Network Time Protocol (SNTP), in RFC-1361 [4].
  47.  
  48.    The model has been implemented in three Unix kernels for Sun
  49.    Microsystems and Digital Equipment workstations. In addition, for the
  50.    Digital machines the model provides improved precision to one
  51.    microsecond (us). Since these specific implementations involve
  52.    modifications to licensed code, they cannot be provided directly.
  53.    Inquiries should be directed to the manufacturer's representatives.
  54.    However, the engineering model for these implementations, including a
  55.  
  56.  
  57.  
  58. Mills                                                           [Page 1]
  59.  
  60. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  61.  
  62.  
  63.    simulator with code segments almost identical to the implementations,
  64.    but not involving licensed code, is available via anonymous FTP from
  65.    host louie.udel.edu in the directory pub/ntp and compressed tar
  66.    archive kernel.tar.Z. The NTP Version 3 distribution can be obtained
  67.    via anonymous ftp from the same host and directory in the compressed
  68.    tar archive xntp3.3g.tar.Z, where the version number shown as 3.3g
  69.    may be adjusted for new versions as they occur.
  70.  
  71. 1. Introduction
  72.  
  73.    This memorandum describes a model and programming interface for
  74.    generic operating system software that manages the system clock and
  75.    timer functions. The model provides improved accuracy and stability
  76.    for most workstations and servers using the Network Time Protocol
  77.    (NTP) or similar time synchronization protocol. This memorandum
  78.    describes the principles of design and implementation of the model.
  79.    Related technical reports discuss the design approach, engineering
  80.    analysis and performance evaluation of the model as implemented in
  81.    Unix kernels for Sun Microsystems and Digital Equipment workstations.
  82.    The NTP Version 3 daemon xntpd operates with these implementations to
  83.    provide improved accuracy and stability, together with diminished
  84.    overhead in the operating system and network. In addition, the model
  85.    supports the use of external timing sources, such as precision
  86.    pulse-per-second (PPS) signals and the industry standard IRIG timing
  87.    signals. The NTP daemon automatically detects the presence of the new
  88.    features and utilizes them when available.
  89.  
  90.    There are three prototype implementations of the model presented in
  91.    this memorandum, one each for the Sun Microsystems SPARCstation with
  92.    the SunOS 4.1.x kernel, Digital Equipment DECstation 5000 with the
  93.    Ultrix 4.x kernel and Digital Equipment 3000 AXP Alpha with the OSF/1
  94.    V1.x kernel. In addition, for the DECstation 5000/240 and 3000 AXP
  95.    Alpha machines, a special feature provides improved precision to 1 us
  96.    (Sun 4.1.x kernels already do provide 1-us precision). Other than
  97.    improving the system clock accuracy, stability and precision, these
  98.    implementations do not change the operation of existing Unix system
  99.    calls which manage the system clock, such as gettimeofday(),
  100.    settimeofday() and adjtime(); however, if the new features are in
  101.    use, the operations of gettimeofday() and adjtime() can be controlled
  102.    instead by new system calls ntp_gettime() and ntp_adjtime() as
  103.    described below.
  104.  
  105.    A detailed description of the variables and algorithms is given in
  106.    the hope that similar functionality can be incorporated in Unix
  107.    kernels for other machines. The algorithms involve only minor changes
  108.    to the system clock and interval timer routines and include
  109.    interfaces for application programs to learn the system clock status
  110.    and certain statistics of the time synchronization process. Detailed
  111.  
  112.  
  113.  
  114. Mills                                                           [Page 2]
  115.  
  116. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  117.  
  118.  
  119.    installation instructions are given in a specific README files
  120.    included in the kernel distributions.
  121.  
  122.    In this memorandum, NTP Version 3 and the Unix implementation xntp3
  123.    are used as an example application of the new system calls for use by
  124.    a synchronization daemon. In principle, the new system calls can be
  125.    used by other protocols and implementations as well. Even in cases
  126.    where the local time is maintained by periodic exchanges of messages
  127.    at relatively long intervals, such as using the NIST Automated
  128.    Computer Time Service, the ability to precisely adjust the system
  129.    clock frequency simplifies the synchronization procedures and allows
  130.    the telephone call frequency to be considerably reduced.
  131.  
  132. 2. Design Approach
  133.  
  134.    While not strictly necessary for an understanding or implementation
  135.    of the model, it may be helpful to briefly describe how NTP operates
  136.    to control the system clock in a client workstation. As described in
  137.    [1], the NTP protocol exchanges timestamps with one or more peers
  138.    sharing a synchronization subnet to calculate the time offsets
  139.    between peer clocks and the local clock. These offsets are processed
  140.    by several algorithms which refine and combine the offsets to produce
  141.    an ensemble average, which is then used to adjust the local clock
  142.    time and frequency. The manner in which the local clock is adjusted
  143.    represents the main topic of this memorandum. The goal in the
  144.    enterprise is the most accurate and stable system clock possible with
  145.    the available kernel software and workstation hardware.
  146.  
  147.    In order to understand how the new software works, it is useful to
  148.    review how most Unix kernels maintain the system time. In the Unix
  149.    design a hardware counter interrupts the kernel at a fixed rate: 100
  150.    Hz in the SunOS kernel, 256 Hz in the Ultrix kernel and 1024 Hz in
  151.    the OSF/1 kernel. Since the Ultrix timer interval (reciprocal of the
  152.    rate) does not evenly divide one second in microseconds, the Ultrix
  153.    kernel adds 64 microseconds once each second, so the timescale
  154.    consists of 255 advances of 3906 us plus one of 3970 us. Similarly,
  155.    the OSF/1 kernel adds 576 us once each second, so its timescale
  156.    consists of 1023 advances of 976 us plus one of 1552 us.
  157.  
  158.    2.1. Mechanisms to Adjust Time and Frequency
  159.  
  160.       In most Unix kernels it is possible to slew the system clock to a
  161.       new offset relative to the current time by using the adjtime()
  162.       system call. To do this the clock frequency is changed by adding
  163.       or subtracting a fixed amount (tickadj) at each timer interrupt
  164.       (tick) for a calculated number of ticks. Since this calculation
  165.       involves dividing the requested offset by tickadj, it is possible
  166.       to slew to a new offset with a precision only of tickadj, which is
  167.  
  168.  
  169.  
  170. Mills                                                           [Page 3]
  171.  
  172. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  173.  
  174.  
  175.       usually in the neighborhood of 5 us, but sometimes much more. This
  176.       results in a roundoff error which can accumulate to an
  177.       unacceptable degree, so that special provisions must be made in
  178.       the clock adjustment procedures of the synchronization daemon.
  179.  
  180.       In order to implement a frequency-discipline function, it is
  181.       necessary to provide time offset adjustments to the kernel at
  182.       regular adjustment intervals using the adjtime() system call. In
  183.       order to reduce the system clock jitter to the regime considered
  184.       in this memorandum, it is necessary that the adjustment interval
  185.       be relatively small, in the neighborhood of 1 s. However, the Unix
  186.       adjtime() implementation requires each offset adjustment to
  187.       complete before another one can be begun, which means that large
  188.       adjustments must be amortized in possibly many adjustment
  189.       intervals. The requirement to implement the adjustment interval
  190.       and compensate for roundoff error considerably complicates the
  191.       synchronizing daemon implementation.
  192.  
  193.       In the new model this scheme is replaced by another that
  194.       represents the system clock as a multiple-word, precision-time
  195.       variable in order to provide very precise clock adjustments. At
  196.       each timer interrupt a precisely calibrated quantity is added to
  197.       the kernel time variable and overflows propagated as required. The
  198.       quantity is computed as in the NTP local clock model described in
  199.       [3], which operates as an adaptive-parameter, first-order, type-II
  200.       phase-lock loop (PLL). In principle, this PLL design can provide
  201.       precision control of the system clock oscillator within 1 us and
  202.       frequency to within parts in 10^11. While precisions of this order
  203.       are surely well beyond the capabilities of the CPU clock
  204.       oscillator used in typical workstations, they are appropriate
  205.       using precision external oscillators as described below.
  206.  
  207.       The PLL design is identical to the one originally implemented in
  208.       NTP and described in [3]. In this design the software daemon
  209.       simulates the PLL using the adjtime() system call; however, the
  210.       daemon implementation is considerably complicated by the
  211.       considerations described above. The modified kernel routines
  212.       implement the PLL in the kernel using precision time and frequency
  213.       representions, so that these complications are avoided. A new
  214.       system call ntp_adjtime() is called only as each new time update
  215.       is determined, which in NTP occurs at intervals of from 16 s to
  216.       1024 s. In addition, doing frequency compensation in the kernel
  217.       means that the system time runs true even if the daemon were to
  218.       cease operation or the network paths to the primary
  219.       synchronization source fail.
  220.  
  221.       In the new model the new ntp_adjtime() operates in a way similar
  222.       to the original adjtime() system call, but does so independently
  223.  
  224.  
  225.  
  226. Mills                                                           [Page 4]
  227.  
  228. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  229.  
  230.  
  231.       of adjtime(), which continues to operate in its traditional
  232.       fashion. When used with NTP, it is the design intent that
  233.       settimeofday() or adjtime() be used only for system time
  234.       adjustments greater than +-128 ms, although the dynamic range of
  235.       the new model is much larger at +-512 ms. It has been the Internet
  236.       experience that the need to change the system time in increments
  237.       greater than +-128 ms is extremely rare and is usually associated
  238.       with a hardware or software malfunction or system reboot.
  239.  
  240.       The easiest way to set the time is with the settimeofday() system
  241.       call; however, this can under some conditions cause the clock to
  242.       jump backward. If this cannot be tolerated, adjtime() can be used
  243.       to slew the clock to the new value without running backward or
  244.       affecting the frequency discipline process. Once the system clock
  245.       has been set within +-128 ms, the ntp_adjtime() system call is
  246.       used to provide periodic updates including the time offset,
  247.       maximum error, estimated error and PLL time constant. With NTP the
  248.       update interval depends on the measured dispersion and time
  249.       constant; however, the scheme is quite forgiving and neither
  250.       moderate loss of updates nor variations in the update interval are
  251.       serious.
  252.  
  253.    2.2 Daemon and Application Interface
  254.  
  255.       Unix application programs can read the system clock using the
  256.       gettimeofday() system call, which returns only the system time and
  257.       timezone data. For some applications it is useful to know the
  258.       maximum error of the reported time due to all causes, including
  259.       clock reading errors, oscillator frequency errors and accumulated
  260.       latencies on the path to a primary synchronization source.
  261.       However, in the new model the PLL adjusts the system clock to
  262.       compensate for its intrinsic frequency error, so that the time
  263.       errors expected in normal operation will usually be much less than
  264.       the maximum error. The programming interface includes a new system
  265.       call ntp_gettime(), which returns the system time, as well as the
  266.       maximum error and estimated error. This interface is intended to
  267.       support applications that need such things, including distributed
  268.       file systems, multimedia teleconferencing and other real-time
  269.       applications. The programming interface also includes the new
  270.       system call ntp_adjtime() mentioned previously, which can be used
  271.       to read and write kernel variables for time and frequency
  272.       adjustment, PLL time constant, leap-second warning and related
  273.       data.
  274.  
  275.       In addition, the kernel adjusts the maximum error to grow by an
  276.       amount equal to the oscillator frequency tolerance times the
  277.       elapsed time since the last update. The default engineering
  278.       parameters have been optimized for update intervals in the order
  279.  
  280.  
  281.  
  282. Mills                                                           [Page 5]
  283.  
  284. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  285.  
  286.  
  287.       of 64 s. For other intervals the PLL time constant can be adjusted
  288.       to optimize the dynamic response over intervals of 16-1024 s.
  289.       Normally, this is automatically done by NTP. In any case, if
  290.       updates are suspended, the PLL coasts at the frequency last
  291.       determined, which usually results in errors increasing only to a
  292.       few tens of milliseconds over a day using room-temperature quartz
  293.       oscillators of typical modern workstations.
  294.  
  295.       While any synchronization daemon can in principle be modified to
  296.       use the new system calls, the most likely will be users of the NTP
  297.       Version 3 daemon xntpd. The xntpd code determines whether the new
  298.       system calls are implemented and automatically reconfigures as
  299.       required. When implemented, the daemon reads the frequency offset
  300.       from a file and provides it and the initial time constant via
  301.       ntp_adjtime(). In subsequent calls to ntp_adjtime(), only the time
  302.       offset and time constant are affected. The daemon reads the
  303.       frequency from the kernel using ntp_adjtime() at intervals of
  304.       about one hour and writes it to a system file. This information is
  305.       recovered when the daemon is restarted after reboot, for example,
  306.       so the sometimes extensive training period to learn the frequency
  307.       separately for each system can be avoided.
  308.  
  309.    2.3. Precision Clocks for DECstation 5000/240 and 3000 AXP Alpha
  310.  
  311.       The stock microtime() routine in the Ultrix kernel returns system
  312.       time to the precision of the timer interrupt interval, which is in
  313.       the 1-4 ms range. However, in the DECstation 5000/240 and possibly
  314.       other machines of that family, there is an undocumented IOASIC
  315.       hardware register that counts system bus cycles at a rate of 25
  316.       MHz. The new microtime() routine for the Ultrix kernel uses this
  317.       register to interpolate system time between timer interrupts. This
  318.       results in a precision of 1 us for all time values obtained via
  319.       the gettimeofday() and ntp_gettime() system calls. For the Digital
  320.       Equipment 3000 AXP Alpha, the architecture provides a hardware
  321.       Process Cycle Counter and a machine instruction rpcc to read it.
  322.       This counter operates at the fundamental frequency of the CPU
  323.       clock or some submultiple of it, 133.333 MHz for the 3000/400 for
  324.       example. The new microtime() routine for the OSF/1 kernel uses
  325.       this counter in the same fashion as the Ultrix routine.
  326.  
  327.       In both the Ultrix and OSF/1 kernels the gettimeofday() and
  328.       ntp_gettime() system call use the new microtime() routine, which
  329.       returns the actual interpolated value, but does not change the
  330.       kernel time variable. Therefore, other routines that access the
  331.       kernel time variable directly and do not call either
  332.       gettimeofday(), ntp_gettime() or microtime() will continue their
  333.       present behavior. The microtime() feature is independent of other
  334.       features described here and is operative even if the kernel PLL or
  335.  
  336.  
  337.  
  338. Mills                                                           [Page 6]
  339.  
  340. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  341.  
  342.  
  343.       new system calls have not been implemented.
  344.  
  345.       The SunOS kernel already includes a system clock with 1-us
  346.       resolution; so, in principle, no microtime() routine is necessary.
  347.       An existing kernel routine uniqtime() implements this function,
  348.       but it is coded in the C language and is rather slow at 42-85 us
  349.       per call. A replacement microtime() routine coded in assembler
  350.       language is available in the NTP Version 3 distribution and is
  351.       much faster at about 3 us per call.
  352.  
  353.    2.4. External Time and Frequency Discipline
  354.  
  355.       The overall accuracy of a time synchronization subnet with respect
  356.       to Coordinated Universal Time (UTC) depends on the accuracy and
  357.       stability of the primary synchronization source, usually a radio
  358.       or satellite receiver, and the system clock oscillator of the
  359.       primary server. As discussed in [5], the traditional interface
  360.       using an RS232 protocol and serial port precludes the full
  361.       accuracy of the radio clock. In addition, the poor stability of
  362.       typical CPU clock oscillators limits the accuracy, whether or not
  363.       precision time sources are available. There are, however, several
  364.       ways in which the system clock accuracy and stability can be
  365.       improved to the degree limited only by the accuracy and stability
  366.       of the synchronization source and the jitter of the operating
  367.       system.
  368.  
  369.       Many radio clocks produce special signals that can be used by
  370.       external equipment to precisely synchronize time and frequency.
  371.       Most produce a pulse-per-second (PPS) signal that can be read via
  372.       a modem-control lead of a serial port and some produce a special
  373.       IRIG signal that can be read directly by a bus peripheral, such as
  374.       the KSI/Odetics TPRO IRIG SBus interface, or indirectly via the
  375.       audio codec of some workstations, as described in [5]. In the NTP
  376.       Version 3 distribution, the PPS signal can be used to augment the
  377.       less precise ASCII serial timecode to improve accuracy to the
  378.       order of microseconds. Support is also included in the
  379.       distribution for the TPRO interface as well as the audio codec;
  380.       however, the latter requires a modified kernel audio driver
  381.       contained in the bsd_audio.tar.Z distribution in the same host and
  382.       directory as the NTP Version 3 distribution mentioned previously.
  383.  
  384.       2.4.1. PPS Signal
  385.  
  386.          The NTP Version 3 distribution includes a special ppsclock
  387.          module for the SunOS 4.1.x kernel that captures the PPS signal
  388.          presented via a modem-control lead of a serial port. Normally,
  389.          the ppsclock module produces a timestamp at each transition of
  390.          the PPS signal and provides it to the synchronization daemon
  391.  
  392.  
  393.  
  394. Mills                                                           [Page 7]
  395.  
  396. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  397.  
  398.  
  399.          for integration with the serial ASCII timecode, also produced
  400.          by the radio clock. With the conventional PLL implementation in
  401.          either the daemon or the kernel as described above, the
  402.          accuracy of this scheme is limited by the intrinsic stability
  403.          of the CPU clock oscillator to a millisecond or two, depending
  404.          on environmental temperature variations.
  405.  
  406.          The ppsclock module has been modified to in addition call a new
  407.          kernel routine hardpps() once each second. The kernel routine
  408.          compares the timestamp with a sample of the CPU clock
  409.          oscillator to develop a frequency offset estimate. This offset
  410.          is used to discipline the oscillator frequency, nominally to
  411.          within a few parts in 10^8, which is about two orders of
  412.          magnitude better than the undisciplined oscillator. The new
  413.          feature is conditionally compiled in the code described below
  414.          only if the PPS_SYNC option is used in the kernel configuration
  415.          file.
  416.  
  417.          When using the PPS signal to adjust the time, there is a
  418.          problem with the SunOS implementation which is very delicate to
  419.          fix. The Sun serial port interrupt routine operates at
  420.          interrupt priority level 12, while the timer interrupt routine
  421.          operates at priority 10. Thus, it is possible that the PPS
  422.          signal interrupt can occur during the timer interrupt routine,
  423.          with result that a tick increment can be missed and the
  424.          returned time early by one tick. It may happen that, if the CPU
  425.          clock oscillator is within a few ppm of the PPS oscillator,
  426.          this condition can persist for two or more successive PPS
  427.          interrupts. A useful workaround has been to use a median filter
  428.          to process the PPS sample offsets. In this filter the sample
  429.          offsets in a window of 20 samples are sorted by offset and the
  430.          six highest and six lowest outlyers discarded. The average of
  431.          the eight samples remaining becomes the output of the filter.
  432.  
  433.          The problem is not nearly so serious when using the PPS signal
  434.          to discipline the frequency of the CPU clock oscillator. In
  435.          this case the quantity of interest is the contents of the
  436.          microseconds counter only, which does not depend on the kernel
  437.          time variable.
  438.  
  439.       2.4.2. External Clocks
  440.  
  441.          It is possible to replace the system clock function with an
  442.          external bus peripheral. The TPRO device mentioned previously
  443.          can be used to provide IRIG-synchronized time with a precision
  444.          of 1 us. A driver for this device tprotime.c and header file
  445.          tpro.h are included in the kernel.tar.Z distribution mentioned
  446.          previously. Using this device the system clock is read directly
  447.  
  448.  
  449.  
  450. Mills                                                           [Page 8]
  451.  
  452. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  453.  
  454.  
  455.          from the interface; however, the device does not record the
  456.          year, so special provisions have to be made to obtain the year
  457.          from the kernel time variable and initialize the driver
  458.          accordingly. This feature is conditionally compiled in the code
  459.          described below only if the EXT_CLOCK option is used in the
  460.          kernel configuration file.
  461.  
  462.          While the system clock function is provided directly by the
  463.          microtime() routine in the driver, the kernel time variable
  464.          must be disciplined as well, since not all system timing
  465.          functions use the microtime() routine. This is done by
  466.          measuring the difference between the microtime() clock and
  467.          kernel time variable and using the difference to adjust the
  468.          kernel PLL as if the adjustment were provided by an external
  469.          peer and NTP.
  470.  
  471.          A good deal of error checking is done in the TPRO driver, since
  472.          the system clock is vulnerable to a misbehaving radio clock,
  473.          IRIG signal source, interface cables and TPRO device itself.
  474.          Unfortunately, there is no easy way to utilize the extensive
  475.          diversity and redundancy capabilities available in the NTP
  476.          synchronization daemon. In order to avoid disruptions that
  477.          might occur if the TPRO time is far different from the kernel
  478.          time variable, the latter is used instead of the former if the
  479.          difference between the two exceeds 1000 s; presumably in that
  480.          case operator intervention is required.
  481.  
  482.       2.4.3. External Oscillators
  483.  
  484.          Even if a source of PPS or IRIG signals is not available, it is
  485.          still possible to improve the stability of the system clock
  486.          through the use of a specialized bus peripheral. In order to
  487.          explore the benefits of such an approach, a special SBus
  488.          peripheral caled HIGHBALL has been constructed. The device
  489.          includes a pair of 32-bit hardware counters in Unix timeval
  490.          format, together with a precision, oven-controlled quartz
  491.          oscillator with a stability of a few parts in 10^9. A driver
  492.          for this device hightime.c and header file high.h are included
  493.          in the kernel.tar.Z distribution mentioned previously. This
  494.          feature is conditionally compiled in the code described below
  495.          only if the EXT_CLOCK and HIGHBALL options are used in the
  496.          kernel configuration file.
  497.  
  498.          Unlike the external clock case, where the system clock function
  499.          is provided directly by the microtime() routine in the driver,
  500.          the HIGHBALL counter offsets with respect to UTC must be
  501.          provided first.  This is done using the ordinary kernel PLL,
  502.          but controlling the counter offsets directly, rather than the
  503.  
  504.  
  505.  
  506. Mills                                                           [Page 9]
  507.  
  508. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  509.  
  510.  
  511.          kernel time variable. At first, this might seem to defeat the
  512.          purpose of the design, since the jitter and wander of the
  513.          synchronization source will affect the counter offsets and thus
  514.          the accuracy of the time. However, the jitter is much reduced
  515.          by the PLL and the wander is small, especially if using a radio
  516.          clock or another primary server disciplined in the same way.
  517.          In practice, the scheme works to reduce the incidental wander
  518.          to a few parts in 10^8, or about the same as using the PPS
  519.          signal.
  520.  
  521.          As in the previous case, the kernel time variable must be
  522.          disciplined as well, since not all system timing functions use
  523.          the microtime() routine. However, the kernel PLL cannot be used
  524.          for this, since it is already in use providing offsets for the
  525.          HIGHBALL counters. Therefore, a special correction is
  526.          calculated from the difference between the microtime() clock
  527.          and the kernel time variable and used to adjust the kernel time
  528.          variable at the next timer interrupt. This somewhat roundabout
  529.          approach is necessary in order that the adjustment does not
  530.          cause the kernel time variable to jump backwards and possibly
  531.          lose or duplicate a timer event.
  532.  
  533.    2.5 Other Features
  534.  
  535.       It is a design feature of the NTP architecture that the system
  536.       clocks in a synchronization subnet are to read the same or nearly
  537.       the same values before during and after a leap-second event, as
  538.       declared by national standards bodies. The new model is designed
  539.       to implement the leap event upon command by an ntp_adjtime()
  540.       argument. The intricate and sometimes arcane details of the model
  541.       and implementation are discussed in [3] and [5]. Further details
  542.       are given in the technical summary later in this memorandum.
  543.  
  544. 3. Technical Summary
  545.  
  546.    In order to more fully understand the workings of the model, a stand-
  547.    alone simulator kern.c and header file timex.h are included in the
  548.    kernel.tar.Z distribution mentioned previously. In addition, a
  549.    complete C program kern_ntptime.c which implements the ntp_gettime()
  550.    and ntp_adjtime() functions is provided, but with the vendor-specific
  551.    argument-passing code deleted. Since the distribution is somewhat
  552.    large, due to copious comments and ornamentation, it is impractical
  553.    to include a listing of these programs in this memorandum. In any
  554.    case, implementors may choose to snip portions of the simulator for
  555.    use in new kernel designs, but, due to formatting conventions, this
  556.    would be difficult if included in this memorandum.
  557.  
  558.  
  559.  
  560.  
  561.  
  562. Mills                                                          [Page 10]
  563.  
  564. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  565.  
  566.  
  567.    The kern.c program is an implementation of an adaptive-parameter,
  568.    first-order, type-II phase-lock loop. The system clock is implemented
  569.    using a set of variables and algorithms defined in the simulator and
  570.    driven by explicit offsets generated by a driver program also
  571.    included in the program. The algorithms include code fragments almost
  572.    identical to those in the machine-specific kernel implementations and
  573.    operate in the same way, but the operations can be understood
  574.    separately from any licensed source code into which these fragments
  575.    may be integrated. The code fragments themselves are not derived from
  576.    any licensed code. The following discussion assumes that the
  577.    simulator code is available for inspection.
  578.  
  579.    3.1. PLL Simulation
  580.  
  581.       The simulator operates in conformance with the analytical model
  582.       described in [3]. The main() program operates as a driver for the
  583.       fragments hardupdate(), hardclock(), second_overflow(), hardpps()
  584.       and microtime(), although not all functions implemented in these
  585.       fragments are simulated. The program simulates the PLL at each
  586.       timer interrupt and prints a summary of critical program variables
  587.       at each time update.
  588.  
  589.       There are three defined options in the kernel configuration file
  590.       specific to each implementation. The PPS_SYNC option provides
  591.       support for a pulse-per-second (PPS) signal, which is used to
  592.       discipline the frequency of the CPU clock oscillator. The
  593.       EXT_CLOCK option provides support for an external kernel-readable
  594.       clock, such as the KSI/Odetics TPRO IRIG interface or HIGHBALL
  595.       precision oscillator, both for the SBus. The TPRO option provides
  596.       support for the former, while the HIGHBALL option provides support
  597.       for the latter. External clocks are implemented as the microtime()
  598.       clock driver, with the specific source code selected by the kernel
  599.       configuration file.
  600.  
  601.       3.1.1. The hardupdate() Fragment
  602.  
  603.          The hardupdate() fragment is called by ntp_adjtime() as each
  604.          update is computed to adjust the system clock phase and
  605.          frequency. Note that the time constant is in units of powers of
  606.          two, so that multiplies can be done by simple shifts. The phase
  607.          variable is computed as the offset divided by the time
  608.          constant. Then, the time since the last update is computed and
  609.          clamped to a maximum (for robustness) and to zero if
  610.          initializing. The offset is multiplied (sorry about the ugly
  611.          multiply) by the result and divided by the square of the time
  612.          constant and then added to the frequency variable. Note that
  613.          all shifts are assumed to be positive and that a shift of a
  614.          signed quantity to the right requires a little dance.
  615.  
  616.  
  617.  
  618. Mills                                                          [Page 11]
  619.  
  620. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  621.  
  622.  
  623.          With the defines given, the maximum time offset is determined
  624.          by the size in bits of the long type (32 or 64) less the
  625.          SHIFT_UPDATE scale factor (12) or at least 20 bits (signed).
  626.          The scale factor is chosen so that there is no loss of
  627.          significance in later steps, which may involve a right shift up
  628.          to SHIFT_UPDATE bits. This results in a time adjustment range
  629.          over +-512 ms. Since time_constant must be greater than or
  630.          equal to zero, the maximum frequency offset is determined by
  631.          the SHIFT_USEC scale factor (16) or at least 16 bits (signed).
  632.          This results in a frequency adjustment range over +-31,500 ppm.
  633.  
  634.          In the addition step, the value of offset * mtemp is not
  635.          greater than MAXPHASE * MAXSEC = 31 bits (signed), which will
  636.          not overflow a long add on a 32-bit machine. There could be a
  637.          loss of precision due to the right shift of up to 12 bits,
  638.          since time_constant is bounded at 6. This results in a net
  639.          worst-case frequency resolution of about .063 ppm, which is not
  640.          significant for most quartz oscillators. The worst case could
  641.          be realized only if the NTP peer misbehaves according to the
  642.          protocol specification.
  643.  
  644.          The time_offset value is clamped upon entry. The time_phase
  645.          variable is an accumulator, so is clamped to the tolerance on
  646.          every call. This helps to damp transients before the oscillator
  647.          frequency has been determined, as well as to satisfy the
  648.          correctness assertions if the time synchronization protocol or
  649.          implementation misbehaves.
  650.  
  651.       3.1.2. The hardclock() Fragment
  652.  
  653.          The hardclock() fragment is inserted in the hardware timer
  654.          interrupt routine at the point the system clock is to be
  655.          incremented. Previous to this fragment the time_update variable
  656.          has been initialized to the value computed by the adjtime()
  657.          system call in the stock Unix kernel, normally plus/minus the
  658.          tickadj value, which is usually in the order of 5 us. The
  659.          time_phase variable, which represents the instantaneous phase
  660.          of the system clock, is advanced by time_adj, which is
  661.          calculated in the second_overflow() fragment described below.
  662.          If the value of time_phase exceeds 1 us in scaled units,
  663.          time_update is increased by the (signed) excess and time_phase
  664.          retains the residue.
  665.  
  666.          Except in the case of an external oscillator such as the
  667.          HIGHBALL interface, the hardclock() fragment advances the
  668.          system clock by the value of tick plus time_update. However, in
  669.          the case of an external oscillator, the system clock is
  670.          obtained directly from the interface and time_update used to
  671.  
  672.  
  673.  
  674. Mills                                                          [Page 12]
  675.  
  676. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  677.  
  678.  
  679.          discipline that interface instead. However, the system clock
  680.          must still be disciplined as explained previously, so the value
  681.          of clock_cpu computed by the second_overflow() fragment is used
  682.          instead.
  683.  
  684.       3.1.3. The second_overflow() Fragment
  685.  
  686.          The second_overflow() fragment is inserted at the point where
  687.          the microseconds field of the system time variable is being
  688.          checked for overflow. Upon overflow the maximum error
  689.          time_maxerror is increased by time_tolerance to reflect the
  690.          maximum time offset due to oscillator frequency error. Then,
  691.          the increment time_adj to advance the kernel time variable is
  692.          calculated from the (scaled) time_offset and time_freq
  693.          variables updated at the last call to the hardclock() fragment.
  694.  
  695.          The phase adjustment is calculated as a (signed) fraction of
  696.          the time_offset remaining, where the fraction is added to
  697.          time_adj, then subtracted from time_offset. This technique
  698.          provides a rapid convergence when offsets are high, together
  699.          with good resolution when offsets are low. The frequency
  700.          adjustment is the sum of the (scaled) time_freq variable, an
  701.          adjustment necessary when the tick interval does not evenly
  702.          divide one second fixtick and PPS frequency adjustment pps_ybar
  703.          (if configured).
  704.  
  705.          The scheme of approximating exact multiply/divide operations
  706.          with shifts produces good results, except when an exact
  707.          calculation is required, such as when the PPS signal is being
  708.          used to discipling the CPU clock oscillator frequency, as
  709.          described below. As long as the actual oscillator frequency is
  710.          a power of two in seconds, no correction is required. However,
  711.          in the SunOS kernel the clock frequency is 100 Hz, which
  712.          results in an error factor of 0.78. In this case the code
  713.          increases time_adj by a factor of 1.25, which results in an
  714.          overall error less than three percent.
  715.  
  716.          On rollover of the day, the leap-second state machine described
  717.          below  determines whether a second is to be inserted or deleted
  718.          in the timescale. The microtime() routine insures that the
  719.          reported time is always monotonically increasing.
  720.  
  721.       3.1.4. The hardpps() Fragment
  722.  
  723.          The hardpps() fragment is operative only if the PPS_SYNC option
  724.          is specified in the kernel configuration file. It is called
  725.          from the serial port driver or equivalent interface at the on-
  726.          time transition of the PPS signal. The fragment operates as a
  727.  
  728.  
  729.  
  730. Mills                                                          [Page 13]
  731.  
  732. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  733.  
  734.  
  735.          first-order, type-I frequency-lock loop (FLL) controlled by the
  736.          difference between the frequency represented by the pps_ybar
  737.          variable and the frequency of the hardware clock oscillator.
  738.  
  739.          In order to avoid calling the microtime() routine more than
  740.          once for each PPS transition, the interface requires the
  741.          calling program to capture the system time and hardware counter
  742.          contents at the on-time transition of the PPS signal and
  743.          provide a pointer to the timestamp (Unix timeval) and counter
  744.          contents as arguments to the hardpps() call. The hardware
  745.          counter contents can be determined by saving the microseconds
  746.          field of the system time, calling the microtime() routine, and
  747.          subtracting the saved value. If a counter overflow has occured
  748.          during the process, the resulting microseconds value will be
  749.          negative, in which case the caller adds 1000000 to normalize
  750.          the microseconds field.
  751.  
  752.          The frequency of the hardware oscillator can be determined from
  753.          the difference in hardware counter readings at the beginning
  754.          and end of the calibration interval divided by the duration of
  755.          the interval. However, the oscillator frequency tolerance, as
  756.          much as 100 ppm, may cause the difference to exceed the tick
  757.          value, creating an ambiguity. In order to avoid this ambiguity,
  758.          the hardware counter value at the beginning of the interval is
  759.          increased by the current pps_ybar value once each second, but
  760.          computed modulo the tick value. At the end of the interval, the
  761.          difference between this value and the value computed from the
  762.          hardware counter is used as a control signal sample for the
  763.          FLL.
  764.  
  765.          Control signal samples which exceed the frequency tolerance are
  766.          discarded, as well as samples resulting from excessive interval
  767.          duration jitter. Surviving samples are then processed by a
  768.          three-stage median filter. The signal which drives the FLL is
  769.          derived from the median sample, while the average of
  770.          differences between the other two samples is used as a measure
  771.          of dispersion. If the dispersion is below the threshold
  772.          pps_dispmax, the median is used to correct the pps_ybar value
  773.          with a weight expressed as a shift PPS_AVG (2). In addition to
  774.          the averaging function, pps_disp is increased by the amount
  775.          pps_dispinc once each second. The result is that, should the
  776.          dispersion be exceptionally high, or if the PPS signal fails
  777.          for some reason, the pps_disp will eventually exceed
  778.          pps_dispmax and raise an alarm.
  779.  
  780.          Initially, an approximate value for pps_ybar is not known, so
  781.          the duration of the calibration interval must be kept small to
  782.          avoid overflowing the tick. The time difference at the end of
  783.  
  784.  
  785.  
  786. Mills                                                          [Page 14]
  787.  
  788. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  789.  
  790.  
  791.          the calibration interval is measured. If greater than a
  792.          fraction tick/4, the interval is reduced by half. If less than
  793.          this fraction for four successive calibration intervals, the
  794.          interval is doubled. This design automatically adapts to
  795.          nominal jitter in the PPS signal, as well as the value of tick.
  796.          The duration of the calibration interval is set by the
  797.          pps_shift variable as a shift in powers of two. The minimum
  798.          value PPS_SHIFT (2) is chosen so that with the highest CPU
  799.          oscillator frequency 1024 Hz and frequency tolerance 100 ppm
  800.          the tick will not overflow. The maximum value PPS_SHIFTMAX (8)
  801.          is chosen such that the maximum averaging time is about 1000 s
  802.          as determined by measurements of Allan variance [5].
  803.  
  804.          Should the PPS signal fail, the current frequency estimate
  805.          pps_ybar continues to be used, so the nominal frequency remains
  806.          correct subject only to the instability of the undisciplined
  807.          oscillator. The procedure to save and restore the frequency
  808.          estimate works as follows. When setting the frequency from a
  809.          file, the time_freq value is set as the file value minus the
  810.          pps_ybar value; when retrieving the frequency, the two values
  811.          are added before saving in the file. This scheme provides a
  812.          seamless interface should the PPS signal fail or the kernel
  813.          configuration change. Note that the frequency discipline is
  814.          active whether or not the synchronization daemon is active.
  815.          Since all Unix systems take some time after reboot to build a
  816.          running system, usually by that time the discipline process has
  817.          already settled down and the initial transients due to
  818.          frequency discipline have damped out.
  819.  
  820.       3.1.4. External Clock Interface
  821.  
  822.          The external clock driver interface is implemented with two
  823.          routines, microtime(), which returns the current clock time,
  824.          and clock_set(), which furnishes the apparent system time
  825.          derived from the kernel time variable. The latter routine is
  826.          called only when the clock is set using the settimeofday()
  827.          system call, but can be called from within the driver, such as
  828.          when the year rolls over, for example.
  829.  
  830.          In the stock SunOS kernel and modified Ultrix and OSF/1
  831.          kernels, the microtime() routine returns the kernel time
  832.          variable plus an interpolation between timer interrupts based
  833.          on the contents of a hardware counter. In the case of an
  834.          external clock, such as described above, the system clock is
  835.          read directly from the hardware clock registers. Examples of
  836.          external clock drivers are in the tprotime.c and hightime.c
  837.          routines included in the kernel.tar.Z distribution.
  838.  
  839.  
  840.  
  841.  
  842. Mills                                                          [Page 15]
  843.  
  844. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  845.  
  846.  
  847.          The external clock routines return a status code which
  848.          indicates whether the clock is operating correctly and the
  849.          nature of the problem, if not. The return code is interpreted
  850.          by the ntp_gettime() system call, which transitions the status
  851.          state machine to the TIME_ERR state if an error code is
  852.          returned. This is the only error checking implemented for the
  853.          external clock in the present version of the code.
  854.  
  855.       The simulator has been used to check the PLL operation over the
  856.       design envelope of +-512 ms in time error and +-100 ppm in
  857.       frequency error. This confirms that no overflows occur and that
  858.       the loop initially converges in about 15 minutes for timer
  859.       interrupt rates from 50 Hz to 1024 Hz. The loop has a normal
  860.       overshoot of a few percent and a final convergence time of several
  861.       hours, depending on the initial time and frequency error.
  862.  
  863.    3.2. Leap Seconds
  864.  
  865.       It does not seem generally useful in the user application
  866.       interface to provide additional details private to the kernel and
  867.       synchronization protocol, such as stratum, reference identifier,
  868.       reference timestamp and so forth. It would in principle be
  869.       possible for the application to independently evaluate the quality
  870.       of time and project into the future how long this time might be
  871.       "valid." However, to do that properly would duplicate the
  872.       functionality of the synchronization protocol and require
  873.       knowledge of many mundane details of the platform architecture,
  874.       such as the subnet configuration, reachability status and related
  875.       variables. For the curious, the ntp_adjtime() system call can be
  876.       used to reveal some of these mysteries.
  877.  
  878.       However, the user application may need to know whether a leap
  879.       second is scheduled, since this might affect interval calculations
  880.       spanning the event. A leap-warning condition is determined by the
  881.       synchronization protocol (if remotely synchronized), by the
  882.       timecode receiver (if available), or by the operator (if awake).
  883.       This condition is set by the synchronization daemon on the day the
  884.       leap second is to occur (30 June or 31 December, as announced) by
  885.       specifying in a ntp_adjtime() system call a clock status of either
  886.       TIME_DEL, if a second is to be deleted, or TIME_INS, if a second
  887.       is to be inserted. Note that, on all occasions since the inception
  888.       of the leap-second scheme, there has never been a deletion
  889.       occasion, nor is there likely to be one in future. If the value is
  890.       TIME_DEL, the kernel adds one second to the system time
  891.       immediately following second 23:59:58 and resets the clock status
  892.       to TIME_OK. If the value is TIME_INS, the kernel subtracts one
  893.       second from the system time immediately following second 23:59:59
  894.       and resets the clock status to TIME_OOP, in effect causing system
  895.  
  896.  
  897.  
  898. Mills                                                          [Page 16]
  899.  
  900. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  901.  
  902.  
  903.       time to repeat second 59. Immediately following the repeated
  904.       second, the kernel resets the clock status to TIME_OK.
  905.  
  906.       Depending upon the system call implementation, the reported time
  907.       during a leap second may repeat (with the TIME_OOP return code set
  908.       to advertise that fact) or be monotonically adjusted until system
  909.       time "catches up" to reported time. With the latter scheme the
  910.       reported time will be correct before and shortly after the leap
  911.       second (depending on the number of microtime() calls during the
  912.       leap second), but freeze or slowly advance during the leap second
  913.       itself. However, Most programs will probably use the ctime()
  914.       library routine to convert from timeval (seconds, microseconds)
  915.       format to tm format (seconds, minutes,...). If this routine is
  916.       modified to use the ntp_gettime() system call and inspect the
  917.       return code, it could simply report the leap second as second 60.
  918.  
  919.    3.3. Clock Status State Machine
  920.  
  921.       The various options possible with the system clock model described
  922.       in this memorandum require a careful examination of the state
  923.       transitions, status indications and recovery procedures should a
  924.       crucial signal or interface fail. In this section is presented a
  925.       prototype state machine designed to support leap second insertion
  926.       and deletion, as well as reveal various kinds of errors in the
  927.       synchronization process. The states of this machine are decoded as
  928.       follows:
  929.  
  930.       TIME_OK   If an external clock is present, it is working properly
  931.                 and the system clock is derived from it. If no external
  932.                 clock is present, the synchronization daemon is working
  933.                 properly and the system clock is synchronized to a radio
  934.                 clock or one or more peers.
  935.  
  936.       TIME_INS  An insertion of one second in the system clock has been
  937.                 declared following the last second of the current day,
  938.                 but has not yet been executed.
  939.  
  940.       TIME_DEL  A deletion of the last second of the current day has
  941.                 been declared, but not yet executed.
  942.  
  943.       TIME_OOP  An insertion of one second in the system clock has been
  944.                 declared following the last second of the current day.
  945.                 The second is in progress, but not yet completed.
  946.                 Library conversion routines should interpret this second
  947.                 as 23:59:60.
  948.  
  949.  
  950.  
  951.  
  952.  
  953.  
  954. Mills                                                          [Page 17]
  955.  
  956. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  957.  
  958.  
  959.       TIME_BAD  Either (a) the synchronization daemon has declared the
  960.                 protocol is not working properly, (b) all sources of
  961.                 outside synchronization have been lost or (c) an
  962.                 external clock is present and it has just become
  963.                 operational following a non-operational condition.
  964.  
  965.       TIME_ERR  An external clock is present, but is in a non-
  966.                 operational condition.
  967.  
  968.       In all except the TIME_ERR state the system clock is derived from
  969.       either an external clock, if present, or the kernel time variable,
  970.       if not. In the TIME_ERR state the external clock is present, but
  971.       not working properly, so the system clock may be derived from the
  972.       kernel time variable. The following diagram indicates the normal
  973.       transitions of the state machine. Not all valid transitions are
  974.       shown.
  975.  
  976.           +--------+     +--------+     +--------+     +--------+
  977.           |        |     |        |     |        |     |        |
  978.           |TIME_BAD|---->|TIME_OK |<----|TIME_OOP|<----|TIME_INS|
  979.           |        |     |        |     |        |     |        |
  980.           +--------+     +--------+     +--------+     +--------+
  981.                A              A
  982.                |              |
  983.                |              |
  984.           +--------+     +--------+
  985.           |        |     |        |
  986.           |TIME_ERR|     |TIME_DEL|
  987.           |        |     |        |
  988.           +--------+     +--------+
  989.  
  990.       The state machine makes a transition once each second at an
  991.       instant where the microseconds field of the kernel time variable
  992.       overflows and one second is added to the seconds field. However,
  993.       this condition is checked at each timer interrupt, which may not
  994.       exactly coincide with the actual instant of overflow. This may
  995.       lead to some interesting anomalies, such as a status indication of
  996.       a leap second in progress (TIME_OOP) when actually the leap second
  997.       had already expired.
  998.  
  999.       The following state transitions are executed automatically by the
  1000.       kernel:
  1001.  
  1002.       any state -> TIME_ERR   This transition occurs when an external
  1003.                               clock is present and an attempt is made to
  1004.                               read it when in a non-operational
  1005.                               condition.
  1006.  
  1007.  
  1008.  
  1009.  
  1010. Mills                                                          [Page 18]
  1011.  
  1012. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  1013.  
  1014.  
  1015.       TIME_INS -> TIME_OOP    This transition occurs immediately
  1016.                               following second 86,400 of the current day
  1017.                               when an insert-second event has been
  1018.                               declared.
  1019.  
  1020.       TIME_OOP -> TIME_OK     This transition occurs immediately
  1021.                               following second 86,401 of the current
  1022.                               day; that is, one second after entry to
  1023.                               the TIME_OOP state.
  1024.  
  1025.       TIME_DEL -> TIME_OK     This transition occurs immediately
  1026.                               following second 86,399 of the current day
  1027.                               when a delete-second event has been
  1028.                               declared.
  1029.  
  1030.       The following state transitions are executed by specific
  1031.       ntp_adjtime() system calls:
  1032.  
  1033.       TIME_OK -> TIME_INS     This transition occurs as the result of a
  1034.                               ntp_adjtime() system call to declare an
  1035.                               insert-second event.
  1036.  
  1037.       TIME_OK -> TIME_DEL     This transition occurs as the result of a
  1038.                               ntp_adjtime() system call to declare a
  1039.                               delete-second event.
  1040.  
  1041.       any state -> TIME_BAD   This transition occurs as the result of a
  1042.                               ntp_adjtime() system call to declare loss
  1043.                               of all sources of synchronization or in
  1044.                               other cases of error.
  1045.  
  1046.       The following table summarizes the actions just before, during and
  1047.       just after a leap-second event. Each line in the table shows the
  1048.       UTC and NTP times at the beginning of the second. The left column
  1049.       shows the behavior when no leap event is to occur. In the middle
  1050.       column the state machine is in TIME_INS at the end of UTC second
  1051.       23:59:59 and the NTP time has just reached 400. The NTP time is
  1052.       set back one second to 399 and the machine enters TIME_OOP. At the
  1053.       end of the repeated second the machine enters TIME_OK and the UTC
  1054.       and NTP times are again in correspondence. In the right column the
  1055.       state machine is in TIME_DEL at the end of UTC second 23:59:58 and
  1056.       the NTP time has just reached 399. The NTP time is incremented,
  1057.       the machine enters TIME_OK and both UTC and NTP times are again in
  1058.       correspondence.
  1059.  
  1060.  
  1061.  
  1062.  
  1063.  
  1064.  
  1065.  
  1066. Mills                                                          [Page 19]
  1067.  
  1068. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  1069.  
  1070.  
  1071.                    No Leap       Leap Insert    Leap Delete
  1072.                    UTC NTP         UTC NTP        UTC NTP
  1073.               ---------------------------------------------
  1074.               23:59:58|398    23:59:58|398   23:59:58|398
  1075.                       |               |              |
  1076.               23:59:59|399    23:59:59|399   00:00:00|400
  1077.                       |               |              |
  1078.               00:00:00|400    23:59:60|399   00:00:01|401
  1079.                       |               |              |
  1080.               00:00:01|401    00:00:00|400   00:00:02|402
  1081.                       |               |              |
  1082.               00:00:02|402    00:00:01|401   00:00:03|403
  1083.                       |               |              |
  1084.  
  1085.       To determine local midnight without fuss, the kernel code simply
  1086.       finds the residue of the time.tv_sec (or time.tv_sec + 1) value
  1087.       mod 86,400, but this requires a messy divide. Probably a better
  1088.       way to do this is to initialize an auxiliary counter in the
  1089.       settimeofday() routine using an ugly divide and increment the
  1090.       counter at the same time the time.tv_sec is incremented in the
  1091.       timer interrupt routine. For future embellishment.
  1092.  
  1093. 4. Programming Model and Interfaces
  1094.  
  1095.    This section describes the programming model for the synchronization
  1096.    daemon and user application programs. The ideas are based on
  1097.    suggestions from Jeff Mogul and Philip Gladstone and a similar
  1098.    interface designed by the latter. It is important to point out that
  1099.    the functionality of the original Unix adjtime() system call is
  1100.    preserved, so that the modified kernel will work as the unmodified
  1101.    one, should the new features not be in use. In this case the
  1102.    ntp_adjtime() system call can still be used to read and write kernel
  1103.    variables that might be used by a synchronization daemon other than
  1104.    NTP, for example.
  1105.  
  1106.    4.1. The ntp_gettime() System Call
  1107.  
  1108.       The syntax and semantics of the ntp_gettime() call are given in
  1109.       the following fragment of the timex.h header file. This file is
  1110.       identical, except for the SHIFT_HZ define, in the SunOS, Ultrix
  1111.       and OSF/1 kernel distributions. (The SHIFT_HZ define represents
  1112.       the logarithm to the base 2 of the clock oscillator frequency
  1113.       specific to each system type.) Note that the timex.h file calls
  1114.       the syscall.h system header file, which must be modified to define
  1115.       the SYS_ntp_gettime system call specific to each system type. The
  1116.       kernel distributions include directions on how to do this.
  1117.  
  1118.  
  1119.  
  1120.  
  1121.  
  1122. Mills                                                          [Page 20]
  1123.  
  1124. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  1125.  
  1126.  
  1127.       /*
  1128.        * This header file defines the Network Time Protocol (NTP)
  1129.        * interfaces for user and daemon application programs. These are
  1130.        * implemented using private system calls and data structures and
  1131.        * require specific kernel support.
  1132.        *
  1133.        * NAME
  1134.        *   ntp_gettime - NTP user application interface
  1135.        *
  1136.        * SYNOPSIS
  1137.        *   #include <sys/timex.h>
  1138.        *
  1139.        *   int system call(SYS_ntp_gettime, tptr)
  1140.        *
  1141.        *   int SYS_ntp_gettime     defined in syscall.h header file
  1142.        *   struct ntptimeval *tptr pointer to ntptimeval structure
  1143.        *
  1144.        * NTP user interface - used to read kernel clock values
  1145.        * Note: maximum error = NTP synch distance = dispersion + delay /
  1146.        * 2
  1147.        * estimated error = NTP dispersion.
  1148.        */
  1149.       struct ntptimeval {
  1150.            struct timeval time;    /* current time */
  1151.            long maxerror;          /* maximum error (us) */
  1152.            long esterror;          /* estimated error (us) */
  1153.       };
  1154.  
  1155.       The ntp_gettime() system call returns three values in the
  1156.       ntptimeval structure: the current time in unix timeval format plus
  1157.       the maximum and estimated errors in microseconds. While the 32-bit
  1158.       long data type limits the error quantities to something more than
  1159.       an hour, in practice this is not significant, since the protocol
  1160.       itself will declare an unsynchronized condition well below that
  1161.       limit. In the NTP Version 3 specification, if the protocol
  1162.       computes either of these values in excess of 16 seconds, they are
  1163.       clamped to that value and the system clock declared
  1164.       unsynchronized.
  1165.  
  1166.       Following is a detailed description of the ntptimeval structure
  1167.       members.
  1168.  
  1169.  
  1170.  
  1171.  
  1172.  
  1173.  
  1174.  
  1175.  
  1176.  
  1177.  
  1178. Mills                                                          [Page 21]
  1179.  
  1180. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  1181.  
  1182.  
  1183.       struct timeval time;    /* current time */
  1184.  
  1185.          This member returns the current system time, expressed as a
  1186.          Unix timeval structure. The timeval structure consists of two
  1187.          32-bit words; the first returns the number of seconds past 1
  1188.          January 1970, while the second returns the number of
  1189.          microseconds.
  1190.  
  1191.       long maxerror;          /* maximum error (us) */
  1192.  
  1193.          This member returns the time_maxerror kernel variable in
  1194.          microseconds. See the entry for this variable in section 5 for
  1195.          additional information.
  1196.  
  1197.       long esterror;          /* estimated error (us) */
  1198.  
  1199.          This member returns the time_esterror kernel variable in
  1200.          microseconds. See the entry for this variable in section 5 for
  1201.          additional information.
  1202.  
  1203.  
  1204.  
  1205.  
  1206.  
  1207.  
  1208.  
  1209.  
  1210.  
  1211.  
  1212.  
  1213.  
  1214.  
  1215.  
  1216.  
  1217.  
  1218.  
  1219.  
  1220.  
  1221.  
  1222.  
  1223.  
  1224.  
  1225.  
  1226.  
  1227.  
  1228.  
  1229.  
  1230.  
  1231.  
  1232.  
  1233.  
  1234. Mills                                                          [Page 22]
  1235.  
  1236. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  1237.  
  1238.  
  1239.    4.2. The ntp_adjtime() System Call
  1240.  
  1241.       The syntax and semantics of the ntp_adjtime() call are given in
  1242.       the following fragment of the timex.h header file. Note that, as
  1243.       in the ntp_gettime() system call, the syscall.h system header file
  1244.       must be modified to define the SYS_ntp_adjtime system call
  1245.       specific to each system type.
  1246.  
  1247.       /*
  1248.        * NAME
  1249.        *   ntp_adjtime - NTP daemon application interface
  1250.        *
  1251.        * SYNOPSIS
  1252.        *   #include <sys/timex.h>
  1253.        *
  1254.        *   int system call(SYS_ntp_adjtime, mode, tptr)
  1255.        *
  1256.        *   int SYS_ntp_adjtime     defined in syscall.h header file
  1257.        *   struct timex *tptr      pointer to timex structure
  1258.        *
  1259.        * NTP daemon interface - used to discipline kernel clock
  1260.        * oscillator
  1261.        */
  1262.       struct timex {
  1263.           int mode;                /* mode selector */
  1264.           long offset;             /* time offset (us) */
  1265.           long frequency;          /* frequency offset (scaled ppm) */
  1266.           long maxerror;           /* maximum error (us) */
  1267.           long esterror;           /* estimated error (us) */
  1268.           int status;              /* clock command/status */
  1269.           long time_constant;      /* pll time constant */
  1270.           long precision;          /* clock precision (us) (read only)
  1271.                                     */
  1272.           long tolerance;          /* clock frequency tolerance (scaled
  1273.                                     * ppm) (read only) */
  1274.           /*
  1275.            * The following read-only structure members are implemented
  1276.            * only if the PPS signal discipline is configured in the
  1277.            * kernel.
  1278.            */
  1279.           long ybar;               /* frequency estimate (scaled ppm) */
  1280.           long disp;               /* dispersion estimate (scaled ppm)
  1281.                                     */
  1282.           int shift;               /* interval duration (s) (shift) */
  1283.           long calcnt;             /* calibration intervals */
  1284.           long jitcnt;             /* jitter limit exceeded */
  1285.           long discnt;             /* dispersion limit exceeded */
  1286.       };
  1287.  
  1288.  
  1289.  
  1290. Mills                                                          [Page 23]
  1291.  
  1292. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  1293.  
  1294.  
  1295.       The ntp_adjtime() system call is used to read and write certain
  1296.       time-related kernel variables summarized in this and subsequent
  1297.       sections. Writing these variables can only be done in superuser
  1298.       mode. To write a variable, the mode structure member is set with
  1299.       one or more bits, one of which is assigned each of the following
  1300.       variables in turn. The current values for all variables are
  1301.       returned in any case; therefore, a mode argument of zero means to
  1302.       return these values without changing anything.
  1303.  
  1304.       Following is a description of the timex structure members.
  1305.  
  1306.       int mode;               /* mode selector */
  1307.  
  1308.          This is a bit-coded variable selecting one or more structure
  1309.          members, with one bit assigned each member. If a bit is set,
  1310.          the value of the associated member variable is copied to the
  1311.          corresponding kernel variable; if not, the member is ignored.
  1312.          The bits are assigned as given in the following fragment of the
  1313.          timex.h header file. Note that the precision and tolerance are
  1314.          determined by the kernel and cannot be changed by
  1315.          ntp_adjtime().
  1316.  
  1317.          /*
  1318.           * Mode codes (timex.mode)
  1319.           */
  1320.          #define ADJ_OFFSET       0x0001    /* time offset */
  1321.          #define ADJ_FREQUENCY    0x0002    /* frequency offset */
  1322.          #define ADJ_MAXERROR     0x0004    /* maximum time error */
  1323.          #define ADJ_ESTERROR     0x0008    /* estimated time error */
  1324.          #define ADJ_STATUS       0x0010    /* clock status */
  1325.          #define ADJ_TIMECONST    0x0020    /* pll time constant */
  1326.  
  1327.       long offset;            /* time offset (us) */
  1328.  
  1329.          If selected, this member replaces the value of the time_offset
  1330.          kernel variable in microseconds. The absolute value must be
  1331.          less than MAXPHASE microseconds defined in the timex.h header
  1332.          file. See the entry for this variable in section 5 for
  1333.          additional information.
  1334.  
  1335.          If within range and the PPS signal and/or external oscillator
  1336.          are configured and operating properly, the clock status is
  1337.          automatically set to TIME_OK.
  1338.  
  1339.  
  1340.  
  1341.  
  1342.  
  1343.  
  1344.  
  1345.  
  1346. Mills                                                          [Page 24]
  1347.  
  1348. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  1349.  
  1350.  
  1351.       long time_constant;     /* pll time constant */
  1352.  
  1353.          If selected, this member replaces the value of the
  1354.          time_constant kernel variable. The value must be between zero
  1355.          and MAXTC defined in the timex.h header file. See the entry for
  1356.          this variable in section 5 for additional information.
  1357.  
  1358.       long frequency;         /* frequency offset (scaled ppm) */
  1359.  
  1360.          If selected, this member replaces the value of the
  1361.          time_frequency kernel variable. The value is in ppm, with the
  1362.          integer part in the high order 16 bits and fraction in the low
  1363.          order 16 bits. The absolute value must be in the range less
  1364.          than MAXFREQ ppm defined in the timex.h header file. See the
  1365.          entry for this variable in section 5 for additional
  1366.          information.
  1367.  
  1368.       long maxerror;          /* maximum error (us) */
  1369.  
  1370.          If selected, this member replaces the value of the
  1371.          time_maxerror kernel variable in microseconds. See the entry
  1372.          for this variable in section 5 for additional information.
  1373.  
  1374.       long esterror;          /* estimated error (us) */
  1375.  
  1376.          If selected, this member replaces the value of the
  1377.          time_esterror kernel variable in microseconds. See the entry
  1378.          for this variable in section 5 for additional information.
  1379.  
  1380.       int status;             /* clock command/status */
  1381.  
  1382.          If selected, this member replaces the value of the time_status
  1383.          kernel variable. See the entry for this variable in section 5
  1384.          for additional information.
  1385.  
  1386.          In order to set this variable by ntp_adjtime(), either (a) the
  1387.          current clock status must be TIME_OK or (b) the member value is
  1388.          TIME_BAD; that is, the ntp_adjtime() call can always set the
  1389.          clock to the unsynchronized state or, if the clock is running
  1390.          correctly, can set it to any state. In any case, the
  1391.          ntp_adjtime() call always returns the current state in this
  1392.          member, so the caller can determine whether or not the request
  1393.          succeeded.
  1394.  
  1395.  
  1396.  
  1397.  
  1398.  
  1399.  
  1400.  
  1401.  
  1402. Mills                                                          [Page 25]
  1403.  
  1404. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  1405.  
  1406.  
  1407.       long time_constant;     /* pll time constant */
  1408.  
  1409.          If selected, this member replaces the value of the
  1410.          time_constant kernel variable. The value must be between zero
  1411.          and MAXTC defined in the timex.h header file. See the entry for
  1412.          this variable in section 5 for additional information.
  1413.  
  1414.       long precision;         /* clock precision (us) (read only) */
  1415.  
  1416.          This member returns the time_precision kernel variable in
  1417.          microseconds. The variable can be written only by the kernel.
  1418.          See the entry for this variable in section 5 for additional
  1419.          information.
  1420.  
  1421.       long tolerance;         /* clock frequency tolerance (scaled ppm)
  1422.                                */
  1423.  
  1424.          This member returns the time_tolerance kernel variable in
  1425.          microseconds. The variable can be written only by the kernel.
  1426.          See the entry for this variable in section 5 for additional
  1427.          information.
  1428.  
  1429.       long ybar;              /* frequency estimate (scaled ppm) */
  1430.  
  1431.          This member returns the pps_ybar kernel variable in
  1432.          microseconds. The variable can be written only by the kernel.
  1433.          See the entry for this variable in section 5 for additional
  1434.          information.
  1435.  
  1436.       long disp;              /* dispersion estimate (scaled ppm) */
  1437.  
  1438.          This member returns the pps_disp kernel variable in
  1439.          microseconds. The variable can be written only by the kernel.
  1440.          See the entry for this variable in section 5 for additional
  1441.          information.
  1442.  
  1443.       int shift;              /* interval duration (s) (shift) */
  1444.  
  1445.          This member returns the pps_shift kernel variable in
  1446.          microseconds. The variable can be written only by the kernel.
  1447.          See the entry for this variable in section 5 for additional
  1448.          information.
  1449.  
  1450.  
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456.  
  1457.  
  1458. Mills                                                          [Page 26]
  1459.  
  1460. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  1461.  
  1462.  
  1463.       long calcnt;            /* calibration intervals */
  1464.  
  1465.          This member returns the pps_calcnt kernel variable in
  1466.          microseconds. The variable can be written only by the kernel.
  1467.          See the entry for this variable in section 5 for additional
  1468.          information.
  1469.  
  1470.       long jitcnt;            /* jitter limit exceeded */
  1471.  
  1472.          This member returns the pps_jittcnt kernel variable in
  1473.          microseconds. The variable can be written only by the kernel.
  1474.          See the entry for this variable in section 5 for additional
  1475.          information.
  1476.  
  1477.       long discnt;            /* dispersion limit exceeded */
  1478.  
  1479.          This member returns the pps_discnt kernel variable in
  1480.          microseconds. The variable can be written only by the kernel.
  1481.          See the entry for this variable in section 5 for additional
  1482.          information.
  1483.  
  1484.  
  1485.  
  1486.  
  1487.  
  1488.  
  1489.  
  1490.  
  1491.  
  1492.  
  1493.  
  1494.  
  1495.  
  1496.  
  1497.  
  1498.  
  1499.  
  1500.  
  1501.  
  1502.  
  1503.  
  1504.  
  1505.  
  1506.  
  1507.  
  1508.  
  1509.  
  1510.  
  1511.  
  1512.  
  1513.  
  1514. Mills                                                          [Page 27]
  1515.  
  1516. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  1517.  
  1518.  
  1519.    4.3. Command/Status Codes
  1520.  
  1521.       The kernel routines use the system clock status variable
  1522.       time_status, which records whether the clock is synchronized,
  1523.       waiting for a leap second, etc. The value of this variable is
  1524.       returned as the result code by both the ntp_gettime() and
  1525.       ntp_adjtime() system calls. In addition, it can be explicitly read
  1526.       and written using the ntp_adjtime() system call, but can be
  1527.       written only in superuser mode. Values presently defined in the
  1528.       timex.h header file are as follows:
  1529.  
  1530.       /*
  1531.        * Clock command/status codes (timex.status)
  1532.        */
  1533.       #define TIME_OK    0    /* clock synchronized */
  1534.       #define TIME_INS   1    /* insert leap second */
  1535.       #define TIME_DEL   2    /* delete leap second */
  1536.       #define TIME_OOP   3    /* leap second in progress */
  1537.       #define TIME_BAD   4    /* kernel clock not synchronized */
  1538.       #define TIME_ERR   5    /* external oscillator not
  1539.                                  synchronized */
  1540.  
  1541.       A detailed description of these codes as used by the leap-second
  1542.       state machine is given later in this memorandum. In case of a
  1543.       negative result code, the kernel has intercepted an invalid
  1544.       address or (in case of the ntp_adjtime() system call), a superuser
  1545.       violation.
  1546.  
  1547. 5. Kernel Variables
  1548.  
  1549.    This section contains a list of kernel variables and a detailed
  1550.    description of their function, initial value, scaling and limits.
  1551.  
  1552.    5.1. Interface Variables
  1553.  
  1554.       The following variables are read and set by the ntp_adjtime()
  1555.       system call. Additional automatic variables are used as
  1556.       temporaries as described in the code fragments.
  1557.  
  1558.       int time_status = TIME_BAD;
  1559.  
  1560.          This variable controls the state machine used to insert or
  1561.          delete leap seconds and show the status of the timekeeping
  1562.          system, PPS signal and external oscillator, if configured.
  1563.  
  1564.  
  1565.  
  1566.  
  1567.  
  1568.  
  1569.  
  1570. Mills                                                          [Page 28]
  1571.  
  1572. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  1573.  
  1574.  
  1575.       long time_offset = 0;
  1576.  
  1577.          This variable is used by the PLL to adjust the system time in
  1578.          small increments. It is scaled by (1 << SHIFT_UPDATE) (12) in
  1579.          microseconds. The maximum value that can be represented is
  1580.          about +-512 ms and the minimum value or precision is a few
  1581.          parts in 10^10 s.
  1582.  
  1583.       long time_constant = 0;      /* pll time constant */
  1584.  
  1585.          This variable determines the bandwidth or "stiffness" of the
  1586.          PLL. The value is used as a shift between zero and MAXTC (6),
  1587.          with the effective PLL time constant equal to a multiple of (1
  1588.          << time_constant) in seconds. For room-temperature quartz
  1589.          oscillator the recommended default value is 2, which
  1590.          corresponds to a PLL time constant of about 900 s and a maximum
  1591.          update interval of about 64 s. The maximum update interval
  1592.          scales directly with the time constant, so that at the maximum
  1593.          time constant of 6, the update interval can be as large as 1024
  1594.          s.
  1595.  
  1596.          Values of time_constant between zero and 2 can be used if quick
  1597.          convergence is necessary; values between 2 and 6 can be used to
  1598.          reduce network load, but at a modest cost in accuracy. Values
  1599.          above 6 are appropriate only if an external oscillator is
  1600.          present.
  1601.  
  1602.       long time_tolerance = MAXFREQ; /* frequency tolerance (ppm) */
  1603.  
  1604.          This variable represents the maximum frequency error or
  1605.          tolerance in ppm of the particular CPU clock oscillator and is
  1606.          a property of the architecture; however, in principle it could
  1607.          change as result of the presence of external discipline
  1608.          signals, for instance. It is expressed as a positive number
  1609.          greater than zero in parts-per-million (ppm).
  1610.  
  1611.          The recommended value of MAXFREQ is 200 ppm is appropriate for
  1612.          room-temperature quartz oscillators used in typical
  1613.          workstations. However, it can change due to the operating
  1614.          condition of the PPS signal and/or external oscillator. With
  1615.          either the PPS signal or external oscillator, the recommended
  1616.          value for MAXFREQ is 100 ppm.
  1617.  
  1618.  
  1619.  
  1620.  
  1621.  
  1622.  
  1623.  
  1624.  
  1625.  
  1626. Mills                                                          [Page 29]
  1627.  
  1628. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  1629.  
  1630.  
  1631.       long time_precision = 1000000 / HZ; /* clock precision (us) */
  1632.  
  1633.          This variable represents the maximum error in reading the
  1634.          system clock in microseconds. It is usually based on the number
  1635.          of microseconds between timer interrupts, 10000 us for the
  1636.          SunOS kernel, 3906 us for the Ultrix kernel, 976 us for the
  1637.          OSF/1 kernel. However, in cases where the time can be
  1638.          interpolated between timer interrupts with microsecond
  1639.          resolution, such as in the unmodified SunOS kernel and modified
  1640.          Ultrix and OSF/1 kernels, the precision is specified as 1 us.
  1641.          In cases where a PPS signal or external oscillator is
  1642.          available, the precision can depend on the operating condition
  1643.          of the signal or oscillator. This variable is determined by the
  1644.          kernel for use by the synchronization daemon, but is otherwise
  1645.          not used by the kernel.
  1646.  
  1647.       long time_maxerror = MAXPHASE; /* maximum error */
  1648.  
  1649.          This variable establishes the maximum error of the indicated
  1650.          time relative to the primary synchronization source in
  1651.          microseconds. For NTP, the value is initialized by a
  1652.          ntp_adjtime() call to the synchronization distance, which is
  1653.          equal to the root dispersion plus one-half the root delay. It
  1654.          is increased by a small amount (time_tolerance) each second to
  1655.          reflect the clock frequency tolerance. This variable is
  1656.          computed by the synchronization daemon and the kernel, but is
  1657.          otherwise not used by the kernel.
  1658.  
  1659.       long time_esterror = MAXPHASE; /* estimated error */
  1660.  
  1661.          This variable establishes the expected error of the indicated
  1662.          time relative to the primary synchronization source in
  1663.          microseconds. For NTP, the value is determined as the root
  1664.          dispersion, which represents the best estimate of the actual
  1665.          error of the system clock based on its past behavior, together
  1666.          with observations of multiple clocks within the peer group.
  1667.          This variable is computed by the synchronization daemon and
  1668.          returned in system calls, but is otherwise not used by the
  1669.          kernel.
  1670.  
  1671.  
  1672.  
  1673.  
  1674.  
  1675.  
  1676.  
  1677.  
  1678.  
  1679.  
  1680.  
  1681.  
  1682. Mills                                                          [Page 30]
  1683.  
  1684. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  1685.  
  1686.  
  1687.    5.2. Phase-Lock Loop Variables
  1688.  
  1689.       The following variables establish the state of the PLL and the
  1690.       residual time and frequency offset of the system clock. Additional
  1691.       automatic variables are used as temporaries as described in the
  1692.       code fragments.
  1693.  
  1694.       long time_phase = 0;         /* phase offset (scaled us) */
  1695.  
  1696.          The time_phase variable represents the phase of the kernel time
  1697.          variable at each tick of the clock. This variable is scaled by
  1698.          (1 << SHIFT_SCALE) (23) in microseconds, giving a maximum
  1699.          adjustment of about +-256 us/tick and a resolution less than
  1700.          one part in 10^12.
  1701.  
  1702.       long time_offset = 0;        /* time offset (scaled us) */
  1703.  
  1704.          The time_offset variable represents the time offset of the CPU
  1705.          clock oscillator. It is recalculated as each update to the
  1706.          system clock is received via the hardupdate() routine and at
  1707.          each second in the seconds_overflow routine. This variable is
  1708.          scaled by (1 << SHIFT_UPDATE) (12) in microseconds, giving a
  1709.          maximum adjustment of about +-512 ms and a resolution of a few
  1710.          parts in 10^10 s.
  1711.  
  1712.       long time_freq = 0;          /* frequency offset (scaled ppm) */
  1713.  
  1714.          The time_freq variable represents the frequency offset of the
  1715.          CPU clock oscillator. It is recalculated as each update to the
  1716.          system clock is received via the hardupdate() routine. It can
  1717.          also be set via ntp_adjtime() from a value stored in a file
  1718.          when the synchronization daemon is first started. It can be
  1719.          retrieved via ntp_adjtime() and written to the file about once
  1720.          per hour by the daemon. The time_freq variable is scaled by (1
  1721.          << SHIFT_KF) (16) ppm, giving it a maximum value well in excess
  1722.          of the limit of +-256 ppm imposed by other constraints. The
  1723.          precision of this representation (frequency resolution) is
  1724.          parts in 10^11, which is adequate for all but the best external
  1725.          oscillators.
  1726.  
  1727.       time_adj = 0;                /* tick adjust (scaled 1 / HZ) */
  1728.  
  1729.          The time_adj variable is the adjustment added to the value of
  1730.          tick at each timer interrupt. It is computed once each second
  1731.          from the time_offset, time_freq and, if the PPS signal is
  1732.          present, the ps_ybar variable once each second.
  1733.  
  1734.  
  1735.  
  1736.  
  1737.  
  1738. Mills                                                          [Page 31]
  1739.  
  1740. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  1741.  
  1742.  
  1743.       long time_reftime = 0;       /* time at last adjustment (s) */
  1744.  
  1745.          This variable is the seconds portion of the system time on the
  1746.          last update received by the hardupdate() routine. It is used to
  1747.          compute the time_freq variable as the time since the last
  1748.          update increases.
  1749.  
  1750.       int fixtick = 1000000 % HZ;  /* amortization factor */
  1751.  
  1752.          In the Ultrix and OSF/1 kernels, the interval between timer
  1753.          interrupts does not evenly divide the number of microseconds in
  1754.          the second. In order that the clock runs at a precise rate, it
  1755.          is necessary to introduce an amortization factor into the local
  1756.          timescale. In the original Unix code, the value of fixtick is
  1757.          amortized once each second, introducing an additional source of
  1758.          jitter; in the new model the value is amortized at each tick of
  1759.          the system clock, reducing the jitter by the reciprocal of the
  1760.          clock oscillator frequency. This is not a new kernel variable,
  1761.          but a new use of an existing kernel variable.
  1762.  
  1763.    5.3. Pulse-per-second (PPS) Frequency-Lock Loop Variables
  1764.  
  1765.       The following variables are used only if a pulse-per-second (PPS)
  1766.       signal is available and connected via a modem-control lead, such
  1767.       as produced by the optional ppsclock feature incorporated in the
  1768.       serial port driver. They establish the design parameters of the
  1769.       PPS frequency-lock loop used to discipline the CPU clock
  1770.       oscillator to an external PPS signal. Additional automatic
  1771.       variables are used as temporaries as described in the code
  1772.       fragments.
  1773.  
  1774.       long pps_usec;          /* microseconds at last pps */
  1775.  
  1776.          The pps_usec variable is latched from a high resolution counter
  1777.          or external oscillator at each PPS interrupt. In determining
  1778.          this value, only the hardware counter contents are used, not
  1779.          the contents plus the kernel time variable, as returned by the
  1780.          microtime() routine.
  1781.  
  1782.       long pps_ybar = 0;      /* pps frequency offset estimate */
  1783.  
  1784.          The pps_ybar variable is the average CPU clock oscillator
  1785.          frequency offset relative to the PPS disciplining signal. It is
  1786.          scaled in the same units as the time_freq variable.
  1787.  
  1788.  
  1789.  
  1790.  
  1791.  
  1792.  
  1793.  
  1794. Mills                                                          [Page 32]
  1795.  
  1796. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  1797.  
  1798.  
  1799.       pps_disp = MAXFREQ;     /* dispersion estimate (scaled ppm) */
  1800.  
  1801.          The pps_disp variable represents the average sample dispersion
  1802.          measured over the last three samples. It is scaled in the same
  1803.          units as the time_freq variable.
  1804.  
  1805.       pps_dispmax = MAXFREQ / 2; /* dispersion threshold */
  1806.  
  1807.          The pps_dispmax variable is used as a dispersion threshold. If
  1808.          pps_disp is less than this threshold, the median sample is used
  1809.          to update the pps_ybar estimate; if not, the sample is
  1810.          discarded.
  1811.  
  1812.       pps_dispinc = MAXFREQ >> (PPS_SHIFT + 4); /* pps dispersion
  1813.       increment/sec */
  1814.  
  1815.          The pps_dispinc variable is the increment to add to pps_disp
  1816.          once each second. It is computed such that, if no PPS samples
  1817.          have arrived for several calibration intervals, the value of
  1818.          pps_disp will exceed the pps_dispmax threshold and raise an
  1819.          alarm.
  1820.  
  1821.       int pps_mf[] = {0, 0, 0};    /* pps median filter */
  1822.  
  1823.          The pps-mf[] array is used as a median filter to detect and
  1824.          discard jitter in the PPS signal.
  1825.  
  1826.       int pps_count = 0;           /* pps calibrate interval counter */
  1827.  
  1828.          The pps_count variable measures the length of the calibration
  1829.          interval used to calculate the frequency. It normally counts
  1830.          from zero to the value 1 << pps_shift.
  1831.  
  1832.       pps_shift = PPS_SHIFT;       /* interval duration (s) (shift) */
  1833.  
  1834.          The pps_shift variable determines the duration of the
  1835.          calibration interval, 1 << pps_shift s.
  1836.  
  1837.       pps_intcnt = 0;              /* intervals at current duration */
  1838.  
  1839.          The pps_intcnt variable counts the number of calibration
  1840.          intervals at the current interval duration. It is reset to zero
  1841.          after four intervals and when the interval duration is changed.
  1842.  
  1843.       long pps_calcnt = 0;         /* calibration intervals */
  1844.  
  1845.          The pps_calcnt variable counts the number of calibration
  1846.          intervals.
  1847.  
  1848.  
  1849.  
  1850. Mills                                                          [Page 33]
  1851.  
  1852. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  1853.  
  1854.  
  1855.       long pps_jitcnt = 0;         /* jitter limit exceeded */
  1856.  
  1857.          The pps_jitcnt variable counts the number of resets due to
  1858.          excessive jitter or frequency offset. These resets are
  1859.          usually due to excessive noise in the PPS signal or
  1860.          interface.
  1861.  
  1862.       long pps_discnt = 0;         /* dispersion limit exceeded */
  1863.  
  1864.          The pps_discnt variable counts the number of calibration
  1865.          intervals where the dispersion is above the pps_dispmax
  1866.          limit.  These resets are usually due to excessive frequency
  1867.          wander in the PPS signal source.
  1868.  
  1869.  
  1870.  
  1871.  
  1872.  
  1873.  
  1874.  
  1875.  
  1876.  
  1877.  
  1878.  
  1879.  
  1880.  
  1881.  
  1882.  
  1883.  
  1884.  
  1885.  
  1886.  
  1887.  
  1888.  
  1889.  
  1890.  
  1891.  
  1892.  
  1893.  
  1894.  
  1895.  
  1896.  
  1897.  
  1898.  
  1899.  
  1900.  
  1901.  
  1902.  
  1903.  
  1904.  
  1905.  
  1906. Mills                                                          [Page 34]
  1907.  
  1908. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  1909.  
  1910.  
  1911.    5.4. External Oscillator Variables
  1912.  
  1913.       The following variables are used only if an external oscillator
  1914.       (HIGHBALL or TPRO) is present. Additional automatic variables are
  1915.       used as temporaries as described in the code fragments.
  1916.  
  1917.       int clock_count = 0;         /* CPU clock counter */
  1918.  
  1919.          The clock_count variable counts the seconds between adjustments
  1920.          to the kernel time variable to discipline it to the external
  1921.          clock.
  1922.  
  1923.       struct timeval clock_offset; /* HIGHBALL clock offset */
  1924.  
  1925.          The clock_offset variable defines the offset between system
  1926.          time and the HIGHBALL counters.
  1927.  
  1928.       long clock_cpu = 0;          /* CPU clock adjust */
  1929.  
  1930.          The clock_cpu variable contains the offset between the system
  1931.          clock and the HIGHBALL clock for use in disciplining the kernel
  1932.          time variable.
  1933.  
  1934. 6. Architecture Constants
  1935.  
  1936.    Following is a list of the important architecture constants that
  1937.    establish the response and stability of the PLL and provide maximum
  1938.    bounds on behavior in order to satisfy correctness assertions made in
  1939.    the protocol specification. Additional definitions are given in the
  1940.    timex.h header file.
  1941.  
  1942.    6.1. Phase-lock loop (PLL) definitions
  1943.  
  1944.       The following defines establish the performance envelope of the
  1945.       PLL. They establish the maximum phase error (MAXPHASE), maximum
  1946.       frequency error (MAXFREQ), minimum interval between updates
  1947.       (MINSEC) and maximum interval between updates (MAXSEC). The intent
  1948.       of these bounds is to force the PLL to operate within predefined
  1949.       limits in order to satisfy correctness assertions of the
  1950.       synchronization protocol. An excursion which exceeds these bounds
  1951.       is clamped to the bound and operation proceeds normally. In
  1952.       practice, this can occur only if something has failed or is
  1953.       operating out of tolerance, but otherwise the PLL continues to
  1954.       operate in a stable mode.
  1955.  
  1956.       MAXPHASE must be set greater than or equal to CLOCK.MAX (128 ms),
  1957.       as defined in the NTP specification. CLOCK.MAX establishes the
  1958.       maximum time offset allowed before the system time is reset,
  1959.  
  1960.  
  1961.  
  1962. Mills                                                          [Page 35]
  1963.  
  1964. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  1965.  
  1966.  
  1967.       rather than incrementally adjusted. Here, the maximum offset is
  1968.       clamped to MAXPHASE only in order to prevent overflow errors due
  1969.       to defective programming.
  1970.  
  1971.       MAXFREQ reflects the manufacturing frequency tolerance of the CPU
  1972.       oscillator plus the maximum slew rate allowed by the protocol. It
  1973.       should be set to at least the intrinsic frequency tolerance of the
  1974.       oscillator plus 100 ppm for vernier frequency adjustments. If the
  1975.       kernel frequency discipline code is installed (PPS_SYNC), the CPU
  1976.       oscillator frequency is disciplined to an external source,
  1977.       presumably with negligible frequency error.
  1978.  
  1979.       #define MAXPHASE 512000      /* max phase error (us) */
  1980.       #ifdef PPS_SYNC
  1981.       #define MAXFREQ 100          /* max frequency error (ppm) */
  1982.       #else
  1983.       #define MAXFREQ 200          /* max frequency error (ppm) */
  1984.       #endif /* PPS_SYNC */
  1985.       #define MINSEC 16            /* min interval between updates (s)
  1986.                                     */
  1987.       #define MAXSEC 1200          /* max interval between updates (s)
  1988.                                     */
  1989.  
  1990.    6.2. Pulse-per-second (PPS) Frequency-lock Loop (FLL) Definitions
  1991.  
  1992.       The following defines and declarations are used only if a pulse-
  1993.       per-second (PPS) signal is available and connected via a modem-
  1994.       control lead, such as produced by the optional ppsclock feature
  1995.       incorporated in the serial port driver. They establish the design
  1996.       parameters of the frequency-lock loop (FLL) used to discipline the
  1997.       CPU clock oscillator to the PPS oscillator.
  1998.  
  1999.       PPS_AVG is the averaging constant used to update the FLL from
  2000.       frequency samples measured for each calibration interval.
  2001.       PPS_SHIFT and PPS_SHIFTMAX are the minimum and maximem,
  2002.       respectively, of the calibration interval represented as a power
  2003.       of two. The PPS_DISPINC is the initial increment to pps_disp at
  2004.       each second.
  2005.  
  2006.       #define PPS_AVG 2            /* pps averaging constant (shift) */
  2007.       #define PPS_SHIFT 2          /* min interval duration (s) (shift)
  2008.                                     */
  2009.       #define PPS_SHIFTMAX 6       /* max interval duration (s) (shift)
  2010.                                     */
  2011.       #define PPS_DISPINC 0        /* dispersion increment (us/s) */
  2012.  
  2013.  
  2014.  
  2015.  
  2016.  
  2017.  
  2018. Mills                                                          [Page 36]
  2019.  
  2020. RFC 1589         Kernel Model for Precision Timekeeping       March 1994
  2021.  
  2022.  
  2023.    6.3. External Oscillator Definitions
  2024.  
  2025.       The following definitions and declarations are used only if an
  2026.       external oscillator (HIGHBALL or TPRO) is configured on the
  2027.       system.
  2028.  
  2029.       #define CLOCK_INTERVAL 30    /* CPU clock update interval (s) */
  2030.  
  2031. 7. References
  2032.  
  2033.    [1] Mills, D., "Internet time synchronization: the Network Time
  2034.        Protocol", IEEE Trans. Communications COM-39, 10 (October 1991),
  2035.        1482- 1493. Also in: Yang, Z., and T.A. Marsland (Eds.). Global
  2036.        States and Time in Distributed Systems, IEEE Press, Los Alamitos,
  2037.        CA, 91-102.
  2038.  
  2039.    [2] Mills, D., "Network Time Protocol (Version 3) specification,
  2040.        implementation and analysis", RFC 1305, University of Delaware,
  2041.        March 1992, 113 pp.
  2042.  
  2043.    [3] Mills, D., "Modelling and analysis of computer network clocks",
  2044.        Electrical Engineering Department Report 92-5-2, University of
  2045.        Delaware, May 1992, 29 pp.
  2046.  
  2047.    [4] Mills, D., "Simple Network Time Protocol (SNTP)", RFC 1361,
  2048.        University of Delaware, August 1992, 10 pp.
  2049.  
  2050.    [5] Mills, D., "Precision synchronizatin of computer network clocks",
  2051.        Electrical Engineering Department Report 93-11-1, University of
  2052.        Delaware, November 1993, 66 pp.
  2053.  
  2054. Security Considerations
  2055.  
  2056.    Security issues are not discussed in this memo.
  2057.  
  2058. Author's Address
  2059.  
  2060.    David L. Mills
  2061.    Electrical Engineering Department
  2062.    University of Delaware
  2063.    Newark, DE 19716
  2064.  
  2065.    Phone: (302) 831-8247
  2066.    EMail: mills@udel.edu
  2067.  
  2068.  
  2069.  
  2070.  
  2071.  
  2072.  
  2073.  
  2074. Mills                                                          [Page 37]
  2075.  
  2076.